package com.supermali.behavior.move.land;

import com.supermali.behavior.move.MoveBehavior;
import com.supermali.entity.npc.bullet.Bullet;

/**
 * 子弹移动
 * */
public class BulletMove extends MoveBehavior {

    private Bullet bullet;

    // x方向速度
    private double vx;
    // y方向速度
    private double vy;
    // y运行方向(向下为-1，向上为1)
    private int ydirect;
    // x方向运行(向右为1，向左为-1)
    private int xdirect;
    //向上运行的总时间
    private long upTime;
    // 向上运行时，高度超过这个值都需要反弹
    private static final int MAX_HEIGHT=80;

    public BulletMove(Bullet bullet) {
        this.bullet = bullet;
        this.vx = 200d/3000;
        this.vy = 300d/3000;
        this.xdirect = 1;
        this.ydirect = -1;
        this.upTime=0;
    }

    @Override
    public void move(long delta) {
        double dx = delta*vx*xdirect;
        double dy = delta*vy*ydirect;
        if(ydirect>0){
            upTime+=delta;
            // 判断是否已经到了临界点
            double dh = upTime * vy;
            if(dh>=MAX_HEIGHT){
                upTime=0;
                this.ydirect=-1;
            }
        }else{
            upTime=0;
        }
        double x1 = bullet.getX() + dx;
        double y1 = bullet.getY() + dy;
        bullet.setX(x1);
        bullet.setY(y1);
    }

    @Override
    public void setV(double v) {
        this.vx = v;
    }

    public void setYdirect(int ydirect) {
        this.ydirect = ydirect;
    }

    public void setXdirect(int xdirect) {
        this.xdirect = xdirect;
    }

    @Override
    public void start() {

    }

    @Override
    public boolean isOver() {
        return false;
    }
}
